﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Condition
    {
        private Variable left;
        private string right;      
        private ConditionType conditionType;
        private BooleanVariable boolVar;
        private IntegerVariable intVar;
        private StringVariable stringVar;
        private bool rightValueBool;
        private int rightValueInt;
        private string rightValueString;
                                // Vanya edit start
        public Variable LOperand
        {
            get { return left; }
            set { left = value; }
        }

        public string ROperand
        {
            get { return right; }
            set { right = value; }
        }

        public ConditionType ConType
        {
            get { return conditionType; }
            set { conditionType = value; }
        }

        public Condition() { }          // Vanya edit end
        public Condition(Variable left, string right,ConditionType type)
        {
            this.left = left;
            this.right = right;
            conditionType = type;

            if (!Validate(left, right, type))
            {
                throw new ArgumentException();
            }
        }

        private bool Validate(Variable left, string right, ConditionType condType)
        {
            Type leftType=left.Type;
            Type rightType=null;
            try
            {
                if (leftType.Equals(typeof(bool)))
                {
                    boolVar = BooleanVariable.ConvertFromVariableInstance(left);
                    //boolVar = boolVar as BooleanVariable;
                    if (bool.TryParse(right, out rightValueBool))
                    {
                        rightType = typeof(bool);
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (leftType.Equals(typeof(int)))
                {
                    intVar = IntegerVariable.ConvertFromVariableInstance(left);

                    //intVar =( IntegerVariable)left;
                    if (int.TryParse(right, out rightValueInt))
                    {
                        rightType = typeof(int);
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (leftType.Equals(typeof(string)))
                {
                    rightValueString = right;
                    //m_rightValueString = left.CurrentValue;
                    //m_leftValueString = right;
                    rightType = typeof(string);

                    //stringlVar = new StringVariable(left.Name, left.Description, left.DefaultValue);
                    //stringlVar.CurrentStringValue = left.CurrentValue;
                    
                    stringVar = stringVar as StringVariable;
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            catch (Exception e)
            {
                return false;
            }
            //
            if (rightType != null)
            {
               // ConditionRules tmp = new ConditionRules();
                return (ConditionRules.FindInRules(rightType, leftType, condType) != null);//!
            }
            else
            {
                return false;
            }
        }
        
        
        public bool GetConditionResault()
        {
            Validate(left, right, conditionType);
            if(boolVar!=null)
            {
                return  (conditionType == ConditionType.Equal)?
                        (boolVar.CurrentBoolValue==rightValueBool):
                        (boolVar.CurrentBoolValue != rightValueBool);

            }
            else if (intVar !=null)
            {
                bool ret=false;
                switch (conditionType)
                { 
                    case ConditionType.Equal:
                        ret = intVar.CurrentIntegerValue == rightValueInt;
                        break;
                    case ConditionType.NonEqual:
                        ret = intVar.CurrentIntegerValue != rightValueInt;
                        break;
                    case ConditionType.More:
                        ret = intVar.CurrentIntegerValue > rightValueInt;
                        break;
                    case ConditionType.Less:
                        ret = intVar.CurrentIntegerValue < rightValueInt;
                        break;
                    case ConditionType.MoreEqual:
                        ret = intVar.CurrentIntegerValue >= rightValueInt;
                        break;
                    case ConditionType.LessEqual:
                        ret = intVar.CurrentIntegerValue <= rightValueInt;
                        break;   
                }
                return ret;
            }
            else if (stringVar!=null)
            {
                return (conditionType == ConditionType.Equal) ?
                        (stringVar.CurrentStringValue == rightValueString) :
                        (stringVar.CurrentStringValue != rightValueString);

            }
            else
            {    
                throw new NotImplementedException();
            }
        }
    }

    
}
